<template>
  <div class="app-container">
    <div class="page-title">共享稿件列表 共计稿件 <span>{{ total }}</span></div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="searchValue">
        <el-input
          style="width: 300px"
          v-model="queryParams.searchValue"
          placeholder="搜索文章标题或者id"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="shareFlag">
        <el-select v-model="queryParams.shareFlag" placeholder="全部类型" style="width: 117px">
          <el-option label="已开启共享" value="1"></el-option>
          <el-option label="已关闭共享" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="city">
        <el-select v-model="queryParams.city" placeholder="选择城市" style="width: 103px" @change="getCountyOptions">
          <el-option
            v-for="item in cityOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="county">
        <el-select v-model="queryParams.county" placeholder="全部区县" style="width: 103px">
          <el-option
            v-for="item in countyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery" round style="width: 90px">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" round @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['manuscripts:sharemanuscripts:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['manuscripts:sharemanuscripts:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['manuscripts:sharemanuscripts:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['manuscripts:sharemanuscripts:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="sharemanuscriptsList" @selection-change="handleSelectionChange" stripe>
      <el-table-column label="稿件ID" align="center" prop="articleId" />
      <el-table-column label="文章标题" align="center" prop="title" width="600px">
        <template slot-scope="scope">
          <el-link type="primary" :href="scope.row.h5Html" target="_blank">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="共享时间" align="center" prop="createTime" />
      <el-table-column label="来源" align="center" prop="sourceFromName" />
      <el-table-column label="编辑账号" align="center" prop="createBy" />
      <el-table-column label="被引用次数" align="center" prop="refCount">
        <template slot-scope="scope">
          <el-link type="primary" @click="goToReferCount(scope.row)">{{ scope.row.refCount }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.shareFlag"
            style="color: #E66D09"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="showCancelShareDialog(scope.row.id, scope.row.shareFlag)"
            v-hasPermi="['manuscripts:sharemanuscripts:edit']"
          >取消共享</el-button>
          <el-button
            v-else
            style="color: #1EBC65"
            size="mini"
            type="text"
            icon="el-icon-top-right"
            @click="showOpenShareDialog(scope.row.id, scope.row.shareFlag)"
            v-hasPermi="['manuscripts:sharemanuscripts:edit']"
          >开启共享</el-button>
          <el-button
            style="color: #D01010"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="showDeleteDialog(scope.row.id)"
            v-hasPermi="['manuscripts:sharemanuscripts:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改共享稿件管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章ID" prop="articleId">
          <el-input v-model="form.articleId" placeholder="请输入文章ID，区县代码+新闻ID" />
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="来源代码" prop="sourceFrom">
          <el-input v-model="form.sourceFrom" placeholder="请输入来源代码" />
        </el-form-item>
        <el-form-item label="是否共享" prop="shareFlag">
          <el-input v-model="form.shareFlag" placeholder="请输入是否共享区县，1是0否" />
        </el-form-item>
        <el-form-item label="新闻H5地址" prop="h5Html">
          <el-input v-model="form.h5Html" placeholder="请输入新闻H5地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  取消共享确认弹窗  -->
    <el-dialog
      :visible.sync="cancelShareDialogVisible"
      width="310px"
      center>
      <p style="text-align: center">确定要取消此篇文稿共享？</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelShareDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cancelShare">确 定</el-button>
      </span>
    </el-dialog>

    <!--  开启共享确认弹窗  -->
    <el-dialog
      :visible.sync="openShareDialogVisible"
      width="310px"
      center>
      <p style="text-align: center">确定要开启此篇文稿共享？</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openShareDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="openShare">确 定</el-button>
      </span>
    </el-dialog>

    <!--  删除确认弹窗  -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      width="310px"
      center>
      <p style="text-align: center">确定要删除此篇文稿？</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteDoc">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { getAllAreaData } from "@/api/manuscripts/source";
  import { listSharemanuscripts, getSharemanuscripts, delSharemanuscripts, addSharemanuscripts, updateSharemanuscripts, exportSharemanuscripts } from "@/api/manuscripts/sharemanuscripts";

export default {
  name: "Sharemanuscripts",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 共享稿件管理表格数据
      sharemanuscriptsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // id字典
      idOptions: [],
      // 文章ID，区县代码+新闻ID字典
      articleIdOptions: [],
      // 文章标题字典
      titleOptions: [],
      // 来源代码字典
      sourceFromOptions: [],
      // 是否共享区县，1是0否字典
      shareFlagOptions: [],
      // 新闻H5地址字典
      h5HtmlOptions: [],
      // 创建者字典
      createByOptions: [],
      // 创建时间字典
      createTimeOptions: [],
      // 更新者字典
      updateByOptions: [],
      // 更新时间字典
      updateTimeOptions: [],
      // 城市和区县选项数据
      areasData: [],
      // 城市下拉选项
      cityOptions: [],
      // 区县下拉选项
      countyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        shareFlag: undefined,
        city: undefined,
        county: undefined,
        sourceFrom: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleId: [
          { required: true, message: "文章ID，区县代码+新闻ID不能为空", trigger: "blur" }
        ],
      },
      // 取消共享确认弹窗
      cancelShareDialogVisible: false,
      cancelShareId: '',
      cancelShareFlag: '',
      // 开启共享确认弹窗
      openShareDialogVisible: false,
      openShareId: '',
      openShareFlag: '',
      // 删除确认弹窗
      deleteDialogVisible: false,
      deleteId: '',
    };
  },
  created() {
    this.getList();
    this.getAllAreas();
    // this.getDicts("${column.dictType}").then(response => {
    //   this.idOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.articleIdOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.titleOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.sourceFromOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.shareFlagOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.h5HtmlOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.createByOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.createTimeOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.updateByOptions = response.data;
    // });
    // this.getDicts("${column.dictType}").then(response => {
    //   this.updateTimeOptions = response.data;
    // });
  },
  methods: {
    /** 查询共享稿件管理列表 */
    getList() {
      this.loading = true;
      // 将city,county参数合并为sourceFrom
      this.queryParams.sourceFrom = this.queryParams.county || this.queryParams.city;
      listSharemanuscripts(this.queryParams).then(response => {
        this.sharemanuscriptsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 查询城市和区县选项数据
    getAllAreas() {
      getAllAreaData().then(response => {
        // {
        //   "510100-成都市": ["510100-全部区县", "510104-锦江区"],
        //   "510300-自贡市": ["510300-全部区县", "510302-自流井区"],
        //   ...
        // }
        this.areasData = response.data;
        // 构造城市选项数据
        Object.keys(this.areasData).forEach((item) => {
          this.cityOptions.push({
            value: item.split("-")[0],
            label: item.split("-")[1],
          })
        })
      })
    },

    // 城市change时获取对应的区县选项数据
    getCountyOptions(val) {
      let cityVals = [];
      this.countyOptions = [];
      this.queryParams.county = undefined;
      Object.keys(this.areasData).forEach((item) => {
        cityVals.push(item.split("-")[0]);
      });
      let cityIndex = cityVals.indexOf(val);
      let cityKey = this.cityOptions[cityIndex].value + "-" + this.cityOptions[cityIndex].label;
      this.areasData[cityKey].forEach((item) => {
        this.countyOptions.push({
          value: item.split("-")[0],
          label: item.split("-")[1],
        })
      });
    },

    // 显示取消共享弹窗
    showCancelShareDialog(id, flag) {
      this.cancelShareDialogVisible = true;
      this.cancelShareId = id;
      this.cancelShareFlag = flag === 1 ? 0 : 1;
    },

    // 取消共享
    cancelShare() {
      updateSharemanuscripts({ id: this.cancelShareId, shareFlag: this.cancelShareFlag }).then(response => {
        if (response.code === 200) {
          this.msgSuccess("取消共享成功");
          this.cancelShareDialogVisible = false;
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      })
    },

    // 显示开启共享弹窗
    showOpenShareDialog(id, flag) {
      this.openShareDialogVisible = true;
      this.openShareId = id;
      this.openShareFlag = flag === 0 ? 1 : 0;
    },

    // 开启共享
    openShare() {
      updateSharemanuscripts({ id: this.openShareId, shareFlag: this.openShareFlag }).then(response => {
        if (response.code === 200) {
          this.msgSuccess("开启共享成功");
          this.openShareDialogVisible = false;
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      })
    },

    // 显示删除确认弹窗
    showDeleteDialog(id) {
      this.deleteDialogVisible = true;
      this.deleteId = id;
    },

    // 删除稿件
    deleteDoc() {
      delSharemanuscripts(this.deleteId).then(response => {
        if (response.code === 200) {
          this.msgSuccess("删除成功");
          this.deleteDialogVisible = false;
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      })
    },

    //  跳转到文章被引用次数页面
    goToReferCount({ articleId, title, h5Html, createTime, sourceFromName, createBy }) {
      this.$router.push({
        path: '/referCount/index',
        query: {
          articleId: articleId,
          title: title,
          h5Html: h5Html,
          createTime: createTime,
          sourceFromName: sourceFromName,
          createBy: createBy
        }
      })
    },

    // id字典翻译
    idFormat(row, column) {
      return this.selectDictLabel(this.idOptions, row.id);
    },
    // 文章ID，区县代码+新闻ID字典翻译
    articleIdFormat(row, column) {
      return this.selectDictLabel(this.articleIdOptions, row.articleId);
    },
    // 文章标题字典翻译
    titleFormat(row, column) {
      return this.selectDictLabel(this.titleOptions, row.title);
    },
    // 来源代码字典翻译
    sourceFromFormat(row, column) {
      return this.selectDictLabel(this.sourceFromOptions, row.sourceFrom);
    },
    // 是否共享区县，1是0否字典翻译
    shareFlagFormat(row, column) {
      return this.selectDictLabel(this.shareFlagOptions, row.shareFlag);
    },
    // 新闻H5地址字典翻译
    h5HtmlFormat(row, column) {
      return this.selectDictLabel(this.h5HtmlOptions, row.h5Html);
    },
    // 创建者字典翻译
    createByFormat(row, column) {
      return this.selectDictLabel(this.createByOptions, row.createBy);
    },
    // 创建时间字典翻译
    createTimeFormat(row, column) {
      return this.selectDictLabel(this.createTimeOptions, row.createTime);
    },
    // 更新者字典翻译
    updateByFormat(row, column) {
      return this.selectDictLabel(this.updateByOptions, row.updateBy);
    },
    // 更新时间字典翻译
    updateTimeFormat(row, column) {
      return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        articleId: undefined,
        title: undefined,
        sourceFrom: undefined,
        shareFlag: undefined,
        h5Html: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加共享稿件管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSharemanuscripts(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改共享稿件管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSharemanuscripts(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSharemanuscripts(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除共享稿件管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSharemanuscripts(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有共享稿件管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSharemanuscripts(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
